package com.zhulanhua.blogs.Utils;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class DozerUtils {
    protected Mapper dozerMapper = new DozerBeanMapper();

    public <T, S> T convert(final S s, Class<T> clz) {
        return s == null ? null : this.dozerMapper.map(s, clz);
    }

    public <T, S> List<T> convert(List<S> s, Class<T> clz) {
        return s == null ? null : (List)s.stream().map((vs) -> {
            return this.dozerMapper.map(vs, clz);
        }).collect(Collectors.toList());
    }

    public <T, S> Set<T> convert(Set<S> s, Class<T> clz) {
        return s == null ? null : (Set)s.stream().map((vs) -> {
            return this.dozerMapper.map(vs, clz);
        }).collect(Collectors.toSet());
    }
}
